<?php
/**
 * Created by PhpStorm.
 * User: 1
 * Date: 2016-11-03
 * Time: 17:24
 */

namespace app\mobileweb\controller;
use app\src\banners\logic\BannersLogic;
use app\src\banners\model\Banners;
use app\src\base\utils\BoyeService;
use app\src\base\helper\PageHelper;
use app\src\base\helper\ValidateHelper;
use app\src\index\action\IndexAction;

/**
 * app首页
 * Class IndexDomain
 * @author hebidu <email:346551990@qq.com>
 * @package app\domain
 */
class Index extends  Webview
{
    public function _initialize()
    {
        $this->needValid =true;  //不需要验证用户密码
        parent::_initialize();
    }
    public function buy(){
        $this->success('请下载纺织快车客户端，享受轻松购买','11111');
    }
    public function index(){

        $userinfo =session('userinfo');
        $uid=$userinfo['id'];
        if(empty($uid)) $this->apiReturnErr('获取用户信息失败，请检查登录');
        $p = $this->_post('p','1');
        $device_token = $this->_post('device_token','');
        $device_type  = 'pc';
        $data = array(

            "uid" => $uid,
            'page_index'=>$p,
            'page_size'=>'20'
        );
        $t= array(
            'api_ver'  => 102,
            'notify_id'=> time(),
            'alg'      => 'md5',
            'device_token'=>$device_token,
            'device_type'=>$device_type,
            'type'	   => 'BY_Index_index'
        );

        $data = array_merge($t, $data);
        $service=new Boyeservice();
        $fanhui=$service->callRemote('',$data,false);
        $product=$fanhui['info']['list'];

        $i=0;$q=0;
        foreach($product as $k =>$v){
            if($v['type']=='ad'){
                $ad[$i]=$v['info']['0'];
                $i++;
            }elseif($v['type']=='p'){
                $pro[$q]=$v['info']['0'];
                $q++;
            }
        }

        $this->assign('data',$pro);
        $this->assign('ad',$ad);
        //dump($pro);
        return $this->fetch();

    }
    private function combine($info,$ads){
        $count = $info['count'];
        $list  = $info['list'];
        $total = count($ads) + count($list);
        $tmp   = [];
        $i     = 0;//ads index
        $j     = 0;//list index

        while($i + $j < $total){
            if(rand(0,10) < 5){
                if($j < count($list)){
                    array_push($tmp, ['type' => 'p', 'info' => [$list[$j]]]);
                    $j++;
                }elseif($i < count($ads)) {
                    array_push($tmp, ['type' => 'ad', 'info' => $ads[$i]]);
                    $i++;
                }
            }else{
                if($i < count($ads)) {
                    array_push($tmp, ['type' => 'ad', 'info' =>  $ads[$i]]);
                    $i++;
                }elseif($j < count($list)){
                    array_push($tmp, ['type' => 'p', 'info' => [$list[$j]]]);
                    $j++;
                }
            }
        }

        return ['count'=>$count,'list'=>$tmp];
    }

    /**
     * 向返回数据插入 广告条目
     * @author hebidu <email:346551990@qq.com>
     */
    private function queryAd(){
        //1. 随机获取首页广告 进行插入
        $logic = new BannersLogic();

        $rand = rand(0,2);

        $result = $logic->query(['position'=>Banners::APP_AD],['curpage'=>1,'size'=>$rand]);
        if(!empty($result['info']) && isset($result['info']['list'])){

            //TODO: 支持多图片返回
            $list = $result['info']['list'];
            $tmp = [];
            foreach ($list as $item){
                array_push($tmp,[$item]);
            }

            return $tmp;
        }

        return [];
    }

//不感兴趣
    public function del(){

        $userinfo =session('userinfo');
        $uid=$userinfo['id'];
        if(empty($uid)) $this->apiReturnErr('获取用户信息失败，请检查登录');
        $pid=$this->_get('pid','');

        $data = array(
            "uid" => $uid,
            'id'=>$pid,
            'page_size'=>'20'
        );
        $t= array(
            'api_ver'  => 100,
            'notify_id'=> time(),
            'alg'      => 'md5',
            'device_type'=>'px',
            'type'	   => 'By_Product_dislike'
        );

        $data = array_merge($t, $data);
        $service=new Boyeservice();
        $fanhui=$service->callRemote('',$data,false);
        if(!$fanhui['status']) $this->apiReturnErr('错误');
        $this->apiReturnSuc($fanhui['info']);

    }




}